package cn.cable.monitor.admin.domain.service.impl;

import cn.cable.monitor.admin.domain.model.entity.MonitorDataEntity;
import cn.cable.monitor.admin.domain.model.entity.MonitorDataMapEntity;
import cn.cable.monitor.admin.domain.model.entity.MonitorFlowDesignerEntity;
import cn.cable.monitor.admin.domain.model.valobj.GatherNodeExpressionVO;
import cn.cable.monitor.admin.domain.model.valobj.GatherNodeExpressionVO.Filed;
import cn.cable.monitor.admin.domain.model.valobj.MonitorTreeConfigVO;
import cn.cable.monitor.admin.domain.repository.IMonitorRepository;
import cn.cable.monitor.admin.domain.service.ILogAnalyticalService;
import cn.cable.monitor.admin.types.Constants;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import javax.annotation.Resource;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
import org.springframework.stereotype.Service;

/**
 * @author hsj
 * @date 2024/7/18 14:32
 */
@Service
public class LogAnalyticalServiceImpl implements ILogAnalyticalService {

	@Resource
	private IMonitorRepository repository;

	@Override
	public void doAnalytical(String systemName, String className, String methodName, List<String> logList) throws OgnlException {

		//查询匹配解析节点
		List<GatherNodeExpressionVO> gatherNodeExpressionVOS = repository.queryGatherNodeExpressionVO(
			systemName,
			className,
			methodName
		);

		if (null == gatherNodeExpressionVOS || gatherNodeExpressionVOS.isEmpty()) {
			return;
		}

		for (GatherNodeExpressionVO vo : gatherNodeExpressionVOS) {
			String monitorName = repository.queryMonitoryNameByMonitoryId(vo.getMonitorId());
			List<Filed> filedList = vo.getFileds();
			for (Filed filed : filedList) {
				Integer logIndex = filed.getLogIndex();
				String logName = logList.get(0);
				if (!logName.equals(filed.getLogName())) {
					continue;
				}
				String attributeValue = "";
				String logStr = logList.get(logIndex);
				if ("Object".equals(filed.getLogType())) {
					OgnlContext context = new OgnlContext();
					context.setRoot(JSONObject.parse(logStr));
					Object root = context.getRoot();
					attributeValue = String.valueOf(Ognl.getValue(filed.getAttributeOgnl(), context, root));
				} else {
					attributeValue = logStr.trim();
					if (attributeValue.contains(Constants.COLON)) {
						attributeValue = attributeValue.split(Constants.COLON)[1].trim();
					}
				}
				MonitorDataEntity monitorDataEntity = MonitorDataEntity.builder()
				                                                       .monitorId(vo.getMonitorId())
				                                                       .monitorName(monitorName)
				                                                       .monitorNodeId(vo.getMonitorNodeId())
				                                                       .systemName(vo.getGatherSystemName())
				                                                       .clazzName(vo.getGatherClazzName())
				                                                       .methodName(vo.getGatherMethodName())
				                                                       .attributeName(filed.getAttributeName())
				                                                       .attributeField(filed.getAttributeField())
				                                                       .attributeValue(attributeValue)
				                                                       .build();

				repository.saveMonitoryData(monitorDataEntity);
			}

		}

	}

	@Override
	public List<MonitorDataMapEntity> queryMonitorDataMapEntityList() {

		return repository.queryMonitorDataMapEntityList();
	}

	@Override
	public MonitorTreeConfigVO queryMonitorFlowData(String monitorId) {
		return repository.queryMonitorFlowData(monitorId);
	}

	@Override
	public List<MonitorDataEntity> queryMonitorDataEntityList(MonitorDataEntity monitorData) {
		return repository.queryMonitorDataEntityList(monitorData);
	}

	@Override
	public void updateMonitorFlowDesigner(MonitorFlowDesignerEntity monitorFlowDesignerEntity) {
		repository.updateMonitorFlowDesigner(monitorFlowDesignerEntity);
	}

}
